package com.rin.executor;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author RinMi
 * @since 2023/6/14
 */
public class FillIdRule {

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    public synchronized Long autoIncrement(String lastId) {
        if (lastId == null){
            return 1L;
        }
        if (!StringUtils.isNumeric(lastId)) {
            throw new RuntimeException("无法转化");
        }
        return Long.parseLong(lastId) + 1;
    }

    public synchronized String dateIncrement(String lastId) {
        String curDate = simpleDateFormat.format(new Date());
        if (lastId == null){
            return curDate + "001";
        }
        String numberPart = lastId.substring(8);
        if (!StringUtils.isNumeric(numberPart)) {
            throw new RuntimeException("无法转化");
        }
        StringBuilder stringTmp = new StringBuilder(String.valueOf(Integer.parseInt(numberPart) + 1));
        int num = 3 - stringTmp.length();
        for (int i = 0; i < num; i++){
            stringTmp.insert(0, "0");
        }
        return curDate + stringTmp;
    }
}
